package de.tabuma.shopping.logik;

import de.tabuma.shopping.entity.*;
import javax.jdo.*;
import java.util.logging.*;
import java.util.*;
import java.text.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/07/21 19:37:32 $
 */
public class OrderFacade {
    private static final Logger log = Logger.getLogger(OrderFacade.class.getName());
    
    public static Order getOrder(int orderNr) {
        String keys[] = {"id"};
        String values[] = {orderNr+""};
        List<Order> oList = PMF.get(keys, values, new Order());
        if(oList.size()>0) {
            return oList.get(0);
        }
        return null;
    }

    public static List<Order> getOrderList(String user) {

        List<UserOrderRelation> uorList = UserOrderRelationFacade.getList1(user);
        List<Order> oList = new ArrayList<Order>();
        for(int i=0;i<uorList.size();i++) {
            UserOrderRelation uor = uorList.get(i);
            oList.add(getOrder(uor.getOrderId()));
        }
        return oList;
    }

    public static int setPayedForOrderByToken(String token, String user) {
        String keys[] = {"token"};
        String values[] = {token};
        List<Order> oList = PMF.get(keys, values, new Order());
        Order myOrder = null;
        if(oList.size()!=0) {
            log.fine("There is an order for token == '"+token+"'");
            myOrder = oList.get(0);
        }
        try {
            myOrder.pay();
            doUpdate(myOrder, user);
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return myOrder.getId().intValue();
    }

    public static int getOrderIDByToken(String token) {
        int orderid = -1;
        String keys[] = {"token"};
        String values[] = {token};
        List<Order> oList = PMF.get(keys, values, new Order());
        if(oList.size()!=0) {
            orderid = oList.get(0).getId().intValue();
        }
        return orderid;
    }

    public static double getPriceOfOrder(int orderID, LicensePackageFacade licensePackageFacade) {
        double price = 0.0;

        Order order = getOrder(orderID);
        if(order!=null) {
            List<LicensePackage> list1 = licensePackageFacade.getAllLicensePackageList();
            List<OrderLicensePackageRelation> list2 = OrderLicensePackageRelationFacade.getList(orderID);
            for(int i=0;i<list1.size();i++) {
                for(int j=0;j<list2.size();j++) {
                    if(list1.get(i).getId().intValue()==list2.get(j).getLicensePackageId()) {
                        price += list1.get(i).getPrice();
                    }
                }
            }
        }
        return price;
    }
    
    public static final int ORDER_CANCEL_DATE = 1;
    public static final int ORDER_DATE = 2;
    public static final int ORDER_PAY_DATE = 3;
    public static final int ORDER_USER = 4;
    public static final int ORDER_LICENSEPACKAGELIST = 5;
    public static final int ORDER_TOKEN = 6;

    public static int doCreate(Order order, String user) {
        int id = (int) PMF.doCreate(order, user);
        UserOrderRelation uor = new UserOrderRelation();
        uor.setOrderId(id);
        uor.setUser(user);
        UserOrderRelationFacade.create(uor, user);
        return id;
    }
    public static void doUpdate(Order order, String user) {
        PMF.doUpdate(order, user);
    }
}
